package com.njym.java.security.app.rest;

import com.njym.java.security.app.biz.InvestTimeBiz;
import com.njym.java.security.app.biz.InvestreplaytblBiz;
import com.njym.java.security.app.entity.InvestTime;
import com.njym.java.security.app.entity.Investreplaytbl;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("investreplaytbl")
public class InvestreplaytblController extends BaseController<InvestreplaytblBiz,Investreplaytbl> {


    @Autowired
    private InvestTimeBiz investTimeBiz;

    @RequestMapping(value = "/getList",method = RequestMethod.GET)
    @ResponseBody
    public Object list(String username,String phoneNo,String code,Integer typetblId){
        List<Investreplaytbl> investreplaytblList = baseBiz.queryByParam(username,phoneNo, code,typetblId);

        return new ObjectRestResponse<>().rel(true).data(investreplaytblList);
    }


    /**
     * 问卷用户最后评分
     * @param appUserId
     * @param phoneNo
     * @param totalScore
     * @param content
     * @param username
     * @param code
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    @ResponseBody
    public Object add(Integer appUserId, String phoneNo, Integer totalScore, String content,
                      String username,String code,Integer typetblId
    ){
        List<Investreplaytbl> investreplaytblList = baseBiz.queryByParams(username,phoneNo,code,typetblId);
        // 判断问卷结果是否重复插入
        if(investreplaytblList.size() == 0){
            InvestTime investTime = investTimeBiz.getList(code,typetblId);
            Investreplaytbl investreplaytbl = new Investreplaytbl();
            investreplaytbl.setAppUserId(appUserId);
            investreplaytbl.setName(username);
            investreplaytbl.setPhoneNo(phoneNo);
            investreplaytbl.setContent(content);
            investreplaytbl.setInvestId(investTime.getInvestId());
            investreplaytbl.setTotalScore(totalScore);
            baseBiz.insertSelective(investreplaytbl);
            return new ObjectRestResponse<>().rel(true);
        } else {
            return ResponseUtil.fail(-1, "该用户问卷分数已存在！");
        }

    }
}